/**
 * Created by wengms on 2016/8/12.
 */
define([
    "angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule',
    'angular-strap-tpl'], function (angular,
                                    JSONPrettyFormat) {

    var UnitCaseExpectModule = angular.module("UnitCaseExpectModule", [
        'DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule',
        'mgcrea.ngStrap']);

    UnitCaseExpectModule.controller("UnitCaseExpectController", function ($scope, Resource, $dataSourceManager, $modal, $location) {

        var CMD = {
                ADD: "ADD",
                EDIT: "EDIT",
                VIEW: "VIEW"
            },
            unitCaseId = $location.search().unitCaseId,
            UnitCaseAssetExpects = Resource("/grule-manager/unitcases/assets/:id", {
                id: "@id"
            }),
            UnitCaseResultExpects = Resource("/grule-manager/unitcases/results/:id", {
                id: "@id"
            }),
            assetCrudTpl = $modal({
                scope: $scope,
                contentTemplate: '/html/unitcases/expects/assets-crud',
                placement: "center",
                show: false
            }),
            resultCrudTpl = $modal({
                scope: $scope,
                contentTemplate: '/html/unitcases/expects/result-crud',
                placement: "center",
                show: false
            }),
            show = function (tpl) {
                assetCrudTpl.hide();
                resultCrudTpl.hide();
                tpl.show()
            };


        $scope.unitCaseId = unitCaseId;

        $scope.assetItem = {
            unitCaseId: unitCaseId
        };
        $scope.resultItem = {
            unitCaseId: unitCaseId
        };

        $scope.settings = {
            editing: true,
            cmd: CMD.ADD,
            icon: "glyphicon glyphicon-plus"
        };


        $scope.actions = {
            showAssetDetail: function (row) {
                $scope.settings = {
                    editing: false,
                    cmd: CMD.VIEW,
                    icon: "glyphicon glyphicon-list"
                };
                $scope.assetItem = row;
                show(assetCrudTpl);
            },
            showUpdateAssetView: function (row) {
                $scope.settings = {
                    editing: true,
                    cmd: CMD.EDIT,
                    icon: "glyphicon glyphicon-edit"
                };
                $scope.assetItem = row;
                show(assetCrudTpl);
            },
            showAddAssetView: function () {
                $scope.settings = {
                    editing: true,
                    cmd: CMD.ADD,
                    icon: "glyphicon glyphicon-plus"
                };
                $scope.assetItem = {
                    unitCaseId: unitCaseId
                };
                show(assetCrudTpl);
            },
            createAsset: function () {
                UnitCaseAssetExpects.create($scope.assetItem, function (result) {
                    if (result.success) {
                        alert("用例创建成功.");
                        $scope.actions.refreshAsset();
                    } else {
                        alert("用例创建失败." + result.errorMessages);
                    }
                    assetCrudTpl.hide();
                });
            },
            updateAsset: function () {
                UnitCaseAssetExpects.update($scope.assetItem, function (result) {
                    if (result.success) {
                        alert("用例修改成功.");
                        $scope.actions.refreshAsset();
                    } else {
                        alert("用例修改失败." + result.errorMessages);
                    }
                    assetCrudTpl.hide();
                });
            },
            refreshAsset: function () {
                $dataSourceManager.dataSources["AssetSource"].doRequestData();
            },
            removeAsset: function (row) {
                UnitCaseAssetExpects.remove(row, function (result) {
                    if (result.success) {
                        alert("用例删除成功.");
                        $scope.actions.refreshAsset();
                    } else {
                        alert("用例删除失败." + result.errorMessages);
                    }
                })
            },
            showUpdateResultView: function () {
                $scope.settings = {
                    editing: true,
                    cmd: CMD.EDIT,
                    icon: "glyphicon glyphicon-edit"
                };
                show(resultCrudTpl);
            },
            showAddResultView: function () {
                if ($scope.result.id) {
                    alert("已存在结果集期望，无法新增。");
                    return;
                }
                $scope.settings = {
                    editing: true,
                    cmd: CMD.ADD,
                    icon: "glyphicon glyphicon-plus"
                };
                $scope.result = {
                    unitCaseId: unitCaseId
                };
                show(resultCrudTpl);
            },
            createResult: function () {
                UnitCaseResultExpects.create($scope.result, function (result) {
                    if (result.success) {
                        alert("用例创建成功.");
                        $scope.actions.refreshResult();
                    } else {
                        alert("用例创建失败." + result.errorMessages);
                    }
                    resultCrudTpl.hide();
                });
            },
            updateResult: function () {
                UnitCaseResultExpects.update($scope.result, function (result) {
                    if (result.success) {
                        alert("用例修改成功.");
                        $scope.actions.refreshResult();
                    } else {
                        alert("用例修改失败." + result.errorMessages);
                    }
                    resultCrudTpl.hide();
                });
            },
            refreshResult: function () {
                UnitCaseResultExpects.get({"id":unitCaseId}, function (result) {
                    $scope.result = result;
                });
            },
            removeResult: function () {
                UnitCaseResultExpects.remove($scope.result, function (result) {
                    if (result.success) {
                        alert("用例删除成功.");
                        $scope.result = {};
                    } else {
                        alert("用例删除失败." + result.errorMessages);
                    }
                })
            }
        };

        $scope.actions.refreshResult();

        $scope.saveUnitCaseAsset = function () {
            if ($scope.settings.cmd === CMD.ADD) {
                $scope.actions.createAsset();
            } else if ($scope.settings.cmd === CMD.EDIT) {
                $scope.actions.updateAsset();
            }
        };
        $scope.saveUnitCaseResult = function () {
            if ($scope.settings.cmd === CMD.ADD) {
                $scope.actions.createResult();
            } else if ($scope.settings.cmd === CMD.EDIT) {
                $scope.actions.updateResult();
            }
        };


    });


    return UnitCaseExpectModule;
});